//
//  AreaPickerView.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/27.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "YOAreaPickerView.h"

@interface YOAreaPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>{
    NSString * cityStr;
    NSString * proStr;
}
/**
 *  存放数据的数组
 */
@property (nonatomic,strong)NSArray *citiesArray;
/**
 *  pickerview
 */
@property (strong, nonatomic) UIPickerView *pickerView;

/**
 *  省模型
 */
@property (nonatomic,strong)Utils * selecletPro;
@property (nonatomic,strong) UIToolbar *myToolBar;

@end

@implementation YOAreaPickerView
- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self initPickerView];
    }
    return self;
}
/**
 *  懒加载
 */
-(NSArray *)citiesArray{
    if (!_citiesArray) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities" ofType:@"plist"]];
        NSMutableArray *nmArray = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dic in array) {
            [nmArray addObject:[Utils citiesWithDic:dic]];
        }
        _citiesArray = nmArray;
    }
    return _citiesArray;
}

- (void)initPickerView {
    //遮罩层
    UIView *view_cover = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:view_cover];
    view_cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    //手势移除
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
    [view_cover addGestureRecognizer:gesture];
    
    _pickerView=  [[UIPickerView alloc]initWithFrame:CGRectMake(0, kGScreenHeight + 34, kGScreenWidth, 180.f)];
    _pickerView.showsSelectionIndicator=YES;
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    _pickerView.contentMode = UIViewContentModeScaleToFill;
    _pickerView.backgroundColor = [UIColor whiteColor];
    [view_cover addSubview:_pickerView];

    self.myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, kGScreenHeight, kGScreenWidth, 40)];
    self.myToolBar.backgroundColor = RGB(254, 232, 223);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 54, 34);
    [btn setTitleColor:self.tintColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"确 定" forState:UIControlStateNormal];
    [btn setTitleColor:RGB(255, 39, 83) forState:(UIControlStateNormal)];
    btn.titleLabel.font = kFont15;
    UIBarButtonItem *finishBtn=[[UIBarButtonItem alloc]initWithCustomView:btn];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, 54, 34);
    [btn1 setTitleColor:self.tintColor forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setTitle:@"取 消" forState:UIControlStateNormal];
    [btn1 setTitleColor:RGB(255, 39, 83) forState:(UIControlStateNormal)];
    btn1.titleLabel.font = kFont15;
    UIBarButtonItem *cancelBtn=[[UIBarButtonItem alloc]initWithCustomView:btn1];
    UIBarButtonItem *flexible=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    NSArray *btnArr=[[NSArray alloc]initWithObjects:cancelBtn,flexible,flexible,finishBtn, nil];
    [self.myToolBar setItems:btnArr];
    [view_cover addSubview:self.myToolBar];
    
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.frame = CGRectMake(0, kGScreenHeight - 180.f, kGScreenWidth, 180.f);
        self.myToolBar.frame = CGRectMake(0.0, kGScreenHeight - 180 - 34, self.frame.size.width, 34);
    }];
    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
}

-(void)cancelAction{
    if (self.removeView) {
        self.removeView(@"");
    }
}

-(void)finishAction{
    if (self.removeView) {
        self.removeView([NSString stringWithFormat:@"%@#%@",proStr, cityStr]);
    }
}

#pragma mark --uipickerviewdatasource中的方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return self.citiesArray.count;
    }else{
        
        // 2.如果是第1组，城市 城市的行数就要看当前正在显示的是哪个省
        //获取省的索引
        NSInteger seleProIndx = [pickerView selectedRowInComponent:0];
        //获取当前省的数据
        Utils * selePro = self.citiesArray[seleProIndx];
        //保存当前省的数据
        self.selecletPro = selePro;
        //返回省的城市的个数
        return selePro.cities.count;
        
    }
}
//返回每一行
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        
        //返回省的名字
        Utils * city = self.citiesArray[row];
        return city.name;
    }else{
        //返回保存的省里面的内容
        return self.selecletPro.cities[row];
    }
    
}
//当用户选中某一行是刷新
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [pickerView reloadComponent:1];
    }
    //设置label的内容
    //选择了第几组的行
    NSInteger selePro = [pickerView selectedRowInComponent:0];
    NSInteger seleCity = [pickerView selectedRowInComponent:1];
    
    //获取内容
    Utils * pro = self.citiesArray[selePro];
    //    NSString * city = pro.cities[seleCity];
    //获取保存的省里面的城市
    NSString * city = self.selecletPro.cities[seleCity];
    //赋值
    proStr = pro.name;
    cityStr = city;
}

//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:17]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)dismissView {
    if (self.removeView) {
        self.removeView([NSString stringWithFormat:@"%@#%@",proStr, cityStr]);
    }
}

@end
